package com.laowang.project.system.controller;

import java.util.Date;
import java.util.List;

import com.laowang.common.utils.SecurityUtils;
import com.laowang.framework.web.page.TableDataInfoMy;
import com.laowang.project.system.domain.MyManagerCoupon;
import com.laowang.project.system.domain.WxUser;
import com.laowang.project.system.service.MyManagerCouponService;
import com.laowang.project.system.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MySystemCoupon;
import com.laowang.project.system.service.MySystemCouponService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 系统券Controller
 *
 * @author laowang
 * @date 2023-08-21
 */
@Api(tags = "系统券接口")
@RestController
@RequestMapping("/systemCoupon")
public class MySystemCouponController extends BaseController {
    @Autowired
    private MySystemCouponService mySystemCouponService;

    @Autowired
    private MyManagerCouponService myManagerCouponService;

    @Autowired
    private WxUserService wxUserService;
    /**
     * 查询系统券列表
     */
    @ApiOperation(value = "小程序端 查询系统券列表",notes = "")
    @GetMapping("/web/list")
    public TableDataInfoMy weblist(MySystemCoupon mySystemCoupon) {
        startPage();
        List<MySystemCoupon> list = mySystemCouponService.selectMySystemCouponListWeb(mySystemCoupon);
        return getDataTableMy(list);
    }

    @ApiOperation(value = "小程序端 领券",notes = "")
    @GetMapping("/draw")
    public AjaxResult weblist(Integer id,Integer number) {

        if (number == null){
            return AjaxResult.error("该券的数量不能为空");
        }
        MySystemCoupon mySystemCoupon = mySystemCouponService.selectMySystemCouponById(id);
        if (mySystemCoupon == null){
            return AjaxResult.error("该券不存在");
        }
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        WxUser wxUser = wxUserService.selectWxUserById(mid);
        Integer type = mySystemCoupon.getType();
        if (type.equals(2)){
            Integer vipLevel = wxUser.getVipLevel();
            if (vipLevel == null || wxUser.getExpireTime() == null || wxUser.getExpireTime().before(new Date()) ){
                return AjaxResult.error("会员已到期");
            }
        }

        MyManagerCoupon myManagerCoupon = new MyManagerCoupon();
        myManagerCoupon.setUserId(mid);
        myManagerCoupon.setSystemCouponId(id);
        List<MyManagerCoupon> myManagerCoupons = myManagerCouponService.selectMyManagerCouponList(myManagerCoupon);
        if (myManagerCoupons.size() >0 && mySystemCoupon.getNum()==1 ){
            return AjaxResult.error("已经领过此券");
        }
        if (number>mySystemCoupon.getNum() ){
            return AjaxResult.error("超过领此券的最大数");
        }
        //已领的数量
        Integer maxInt = 0;
        for (MyManagerCoupon managerCoupon : myManagerCoupons) {
            maxInt += managerCoupon.getNum();
        }

        /*if (number > maxInt){
            return AjaxResult.error("超过领此券的最大数");
        }*/
        if (number > mySystemCoupon.getNum() - maxInt  ){
            return AjaxResult.error("超过领此券的最大数");
        }
        myManagerCoupon.setCreated(new Date());
        myManagerCoupon.setDesc(mySystemCoupon.getDesc());
        myManagerCoupon.setPrice(mySystemCoupon.getPrice());
        myManagerCoupon.setNum(number);
        myManagerCoupon.setShopId(mySystemCoupon.getShopId());
        myManagerCoupon.setExpireTime(mySystemCoupon.getEndTime());
        myManagerCoupon.setTitle(mySystemCoupon.getTitle());
        myManagerCoupon.setIsCheck(0);
        myManagerCouponService.insertMyManagerCoupon(myManagerCoupon);
        return AjaxResult.success("操作成功");
    }

    /**
     * 查询系统券列表
     */
    @ApiOperation(value = "查询系统券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MySystemCoupon mySystemCoupon) {
        startPage();
        List<MySystemCoupon> list = mySystemCouponService.selectMySystemCouponList(mySystemCoupon);
        return getDataTable(list);
    }

    /**
     * 导出系统券列表
     */
    @ApiOperation(value = "导出系统券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:export')")
    //@Log(title = "系统券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MySystemCoupon mySystemCoupon) {
        List<MySystemCoupon> list = mySystemCouponService.selectMySystemCouponList(mySystemCoupon);
        ExcelUtil<MySystemCoupon> util = new ExcelUtil<MySystemCoupon>(MySystemCoupon.class);
        return util.exportExcel(list, "systemCoupon");
    }

    /**
     * 获取系统券详细信息
     */
    @ApiOperation(value = "获取系统券详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(mySystemCouponService.selectMySystemCouponById(id));
    }

    /**
     * 新增系统券
     */
    @ApiOperation(value = "新增系统券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:add')")
    //@Log(title = "系统券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MySystemCoupon mySystemCoupon) {
        return toAjax(mySystemCouponService.insertMySystemCoupon(mySystemCoupon));
    }

    /**
     * 修改系统券
     */
    @ApiOperation(value = "修改系统券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:edit')")
    //@Log(title = "系统券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MySystemCoupon mySystemCoupon) {
        return toAjax(mySystemCouponService.updateMySystemCoupon(mySystemCoupon));
    }

    /**
     * 删除系统券
     */
    @ApiOperation(value = "删除系统券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:systemCoupon:remove')")
    //@Log(title = "系统券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(mySystemCouponService.deleteMySystemCouponByIds(ids));
    }
}
